package com.yyx.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.generator.config.IFileCreate;
import com.yyx.server.mapper.JoblevelMapper;
import com.yyx.server.mapper.MailLogMapper;
import com.yyx.server.mapper.PositionMapper;
import com.yyx.server.pojo.*;
import com.yyx.server.mapper.EmployeeMapper;
import com.yyx.server.service.EmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyx.server.service.JoblevelService;
import com.yyx.server.service.MailLogService;
import com.yyx.server.service.PositionService;
import com.yyx.server.vo.MailVo;
import com.yyx.server.vo.PageVo;
import com.yyx.server.vo.PieVo;
import com.yyx.server.vo.ResultVo;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YongXin
 * @since 2021-07-24
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MailLogMapper mailLogMapper;
    @Autowired
    private JoblevelMapper joblevelMapper;
    @Autowired
    private PositionMapper positionMapper;
    /**
     * 查询员工并分页
     * @param currentPage
     * @param pageSize
     * @param employee
     * @param dateScope
     * @return
     */
    @Override
    public IPage<Employee> getEmployeeByPage(Integer currentPage, Integer pageSize, Employee employee, LocalDate[] dateScope) {
        Page<Employee> page = new Page<>(currentPage, pageSize);
        IPage<Employee> employeeIPage = employeeMapper.getEmployeeByPage(page, employee, dateScope);
        return employeeIPage;
    }

    /**
     * 获取工号
     * @return
     */
    @Override
    public String getWorkId() {
        Employee employee = employeeMapper.selectOne(new QueryWrapper<Employee>().orderByDesc("work_id").last("limit 1"));
        return String.format("%08d",Integer.parseInt(employee.getWorkId()) + 1);
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @Override
    public ResultVo addEmp(Employee employee) {
        LocalDateTime beginContract = employee.getBeginContract();
        LocalDateTime endContract = employee.getEndContract();
        long day = beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(day / 365.00)));
        if (employeeMapper.insert(employee) == 1){
            Employee emp = employeeMapper.getEmployee(employee.getId()).get(0);
            String msgId = UUID.randomUUID().toString();
            MailLog mailLog = new MailLog();
            mailLog.setMsgId(msgId);
            mailLog.setEId(employee.getId());
            mailLog.setStatus(0);
            mailLog.setRouteKey(MailConstants.ROUTING_KEY);
            mailLog.setExchange(MailConstants.EXCHANGE);
            mailLog.setCount(0);
            //重试时间为当前时间加上消息超时时间
            mailLog.setTryTime(LocalDateTime.now().plusMinutes(MailConstants.MSG_TIMEOUT));
            mailLog.setCreateTime(LocalDateTime.now());
            mailLog.setUpdateTime(LocalDateTime.now());
            mailLogMapper.insert(mailLog);
            //发送消息
            MailVo rz = new MailVo("","", emp,"rz");
            rabbitTemplate.convertAndSend(MailConstants.EXCHANGE,MailConstants.ROUTING_KEY,rz,new CorrelationData(msgId));
            return ResultVo.success("添加成功");
        }
        return ResultVo.error("添加失败");
    }

    /**
     * 查询员工不分页
     * @param id
     * @return
     */
    @Override
    public List<Employee> getEmployee(Integer id) {
        return employeeMapper.getEmployee(id);
    }

    /**
     * 获取所有员工工资账套
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public IPage<Employee> getEmpWithSalary(Integer currentPage, Integer pageSize) {

        Page<Employee> page = new Page<>(currentPage,pageSize);
       IPage<Employee> employeeIPage = employeeMapper.getEmpWithSalary(page);
       return employeeIPage;
    }

    /**
     * 职称数据统计
     * @return
     */
    @Override
    public List<PieVo> staJobLevel() {
        QueryWrapper<Joblevel> joblevelWrapper = new QueryWrapper<Joblevel>().eq("enabled", 1);
        List<Joblevel> joblevelList = joblevelMapper.selectList(joblevelWrapper);
        ArrayList<PieVo> pieVos = new ArrayList<>();
        for (Joblevel joblevel : joblevelList) {
            QueryWrapper<Employee> countWrapper = new QueryWrapper<Employee>().eq("job_level_id", joblevel.getId());
            Integer count = employeeMapper.selectCount(countWrapper);
            pieVos.add(new PieVo(joblevel.getName(),count));
        }
        return pieVos;
    }

    /**
     * 职位信息统计
     * @return
     */
    @Override
    public Map<String, Integer> staPos() {
        List<Position> positionList = positionMapper.selectList(new QueryWrapper<Position>().eq("enabled", 1));
        System.out.println(positionList);
        HashMap<String, Integer> map = new HashMap<>();
        for (Position position : positionList) {
            System.out.println(position);
            Integer count = employeeMapper.selectCount(new QueryWrapper<Employee>().eq("pos_id", position.getId()));
            System.out.println(count);
            map.put(position.getName(),count);
        }
        return map;
    }

    @Override
    public IPage<Employee> getEmpWithSalaryByWorkId(Integer currentPage, Integer pageSize, String workId) {
        Page<Employee> page = new Page<>(currentPage,pageSize);
        IPage<Employee> employeeIPage = employeeMapper.getEmpWithSalaryByWorkId(page,workId);
        return employeeIPage;
    }
}
